

package ru.samgtu.wst.plugin.proxy;

import java.io.IOException;

import ru.samgtu.wst.httpclient.HTTPClient;
import ru.samgtu.wst.model.Preferences;
import ru.samgtu.wst.model.Request;
import ru.samgtu.wst.model.Response;

public class BrowserCache extends ProxyPlugin {

	private boolean _enabled = false;

	/** Creates a new instance of RevealHidden */
	public BrowserCache() {
		parseProperties();
	}

	public void parseProperties() {
		String prop = "BrowserCache.enabled";
		String value = Preferences.getPreference(prop, "false");
		_enabled = "true".equalsIgnoreCase(value)
				|| "yes".equalsIgnoreCase(value);
	}

	@Override
	public String getPluginName() {
		return new String("Browser Cache");
	}

	public void setEnabled(boolean bool) {
		_enabled = bool;
		String prop = "BrowserCache.enabled";
		Preferences.setPreference(prop, Boolean.toString(bool));
	}

	public boolean getEnabled() {
		return _enabled;
	}

	@Override
	public HTTPClient getProxyPlugin(HTTPClient in) {
		return new Plugin(in);
	}

	private class Plugin implements HTTPClient {

		private HTTPClient _in;

		public Plugin(HTTPClient in) {
			_in = in;
		}

		public Response fetchResponse(Request request) throws IOException {
			if (_enabled) {
				// we could be smarter about this, and keep a record of the
				// pages that we
				// have seen so far, and only remove headers for those that we
				// have not?
				request.deleteHeader("ETag");
				request.deleteHeader("If-Modified-Since");
				request.deleteHeader("If-None-Match");
			}
			return _in.fetchResponse(request);
		}

	}

}
